 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string hash[10] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    string path;
    vector<string> ret;

    vector<string> letterCombinations(string digits) {
        if (digits.size() == 0) return ret;
        dfs(digits, 0);
        return ret;
    }

    void dfs(string& digits, int pos)
    {
        if (pos == digits.size())
        {
            ret.push_back(path);
            return;
        }

        for (auto& str : hash[digits[pos] - '0'])
        {
            path.push_back(str);
            dfs(digits, pos + 1);
            path.pop_back();
        }
    }
};

class Solution {
public:
    int left, right, _n;
    string path;
    vector<string>ret;

    vector<string> generateParenthesis(int n) {
        _n = n;
        dfs();
        return ret;
    }

    void dfs()
    {
        if (right == _n)
        {
            ret.push_back(path);
            return;
        }

        if (left < _n) //添加左括号
        {
            path.push_back('(');
            left++;
            dfs();

            path.pop_back();
            left--;
        }

        if (right < left) //添加右括号
        {
            path.push_back(')');
            right++;
            dfs();

            path.pop_back();
            right--;
        }
    }
};

class Solution {
public:
    int _n, _k;
    vector<int> path;
    vector<vector<int>> ret;

    vector<vector<int>> combine(int n, int k) {
        _n = n, _k = k;
        dfs(1);
        return ret;
    }

    void dfs(int start)
    {
        if (path.size() == _k)
        {
            ret.push_back(path);
            return;
        }

        for (int i = start; i <= _n; i++)
        {
            path.push_back(i);
            dfs(i + 1);
            path.pop_back();
        }
    }
};

class Solution {
public:
    int path, _target;
    int ret;

    int findTargetSumWays(vector<int>& nums, int target) {
        _target = target;
        dfs(nums, 0, 0);
        return ret;
    }

    void dfs(vector<int>& nums, int path, int pos)
    {
        if (pos == nums.size())
        {
            if (path == _target) ret++;
            return;
        }

        //加
        dfs(nums, path + nums[pos], pos + 1);

        //减
        dfs(nums, path - nums[pos], pos + 1);
    }
};

class Solution {
public:
    int aim;
    vector<int> path;
    vector<vector<int>> ret;

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        aim = target;
        //记录总和，还有当前位置
        dfs(candidates, 0, 0);
        return ret;
    }

    void dfs(vector<int>& nums, int pos, int sum)
    {
        if (sum == aim)
        {
            ret.push_back(path);
            return;
        }

        if (sum > aim || pos == nums.size()) return;

        for (int k = 0; k * nums[pos] + sum <= aim; k++)
        {
            if (k) path.push_back(nums[pos]);
            dfs(nums, pos + 1, sum + k * nums[pos]);
        }

        //回溯
        for (int k = 1; k * nums[pos] + sum <= aim; k++)
        {
            path.pop_back();
        }
    }
};